package 包含min函数的栈;

import java.util.LinkedList;

public class MinStackV1 {

    /**
     * 此版本两个栈数量不等, 可能会造成空指针
     */
    private LinkedList<Integer> stack;
    private LinkedList<Integer> minStack;

    /** initialize your data structure here. */
    public MinStackV1() {
        stack = new LinkedList<>();
        minStack = new LinkedList<>();
    }

    public void push(int x) {
        stack.push(x);
        if(minStack.isEmpty() || x < minStack.peek()) {
            minStack.push(x);
        }
    }

    public void pop() {
        Integer popValue = stack.pop();
        if(minStack.isEmpty()) {
            return;
        }
        if(minStack.peek().equals(popValue)) {
            minStack.pop();
        }
    }

    public int top() {
        return stack.peek();
    }

    public int min() {
        return minStack.peek();
    }
}
